<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cookie封装</title>
</head>
<body>
    <script>
        
        // console.log(document.cookie);

        // 1.设置/更改cookie
        function setCookie(name,value,expires){
            var cookie_str = name+'='+value;
            
            if(typeof expires === 'number'){
                var t = new Date();
                t.setDate(t.getDate()+expires);
                // 注意拼接不要少东西
                cookie_str += ";expires="+t;   
            }
            
            document.cookie = cookie_str;
        }
        // setCookie("password","8888888",10);

        // 2.获取cookie
        function getCookie(name){
            var cookie_arr = document.cookie.split('; ');
            for(var i=0;i<cookie_arr.length;i++){
                var cookie_item = cookie_arr[i];
                cookie_item = cookie_item.split('=');
                
                if(cookie_item[0] == name){
                    return cookie_item[1];
                }   
            }
            return '';
        }
        // console.log(getCookie('password'));

        // 3.删除cookie
        function removeCookie(name){
            setCookie(name,"",-1);
        }
    </script>
</body>
</html>